package DB

import java.text.SimpleDateFormat
import java.util.{Date, UUID}

import common._
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

import scala.collection.mutable.HashMap
import common.TimeTool._


/**
  * Created by 林 on 14-3-26.
  */

class BaseDBEntity[+Self <: BaseDBEntity[Self]](tableName: String) extends DBEntity(tableName) {

  def uuid = UUID.randomUUID().toString.replace("-", "")

  def toJson: String = {
    BaseDBEntity.map.writeValueAsString(this)
  }

  def toMap: Map[String, Any] = {
    BaseDBEntity.map.readValue(toJson, Map[String, Any]().getClass).asInstanceOf[Map[String, Any]]
  }

  def toHashMap: HashMap[String, Any] = {
    BaseDBEntity.map.readValue(toJson, HashMap[String, Any]().getClass).asInstanceOf[HashMap[String, Any]]
  }

  def fromJson(json: String): Self = {
    BaseDBEntity.map.readValue(json, this.getClass).asInstanceOf[Self]
  }

  //将对应的更新类转为实体类
  def changeUpdateBean(): Self = {
    fromJson(toJson)
  }

  override def queryById(id: String): Option[Self] = {
    super.queryById(id) map (_.asInstanceOf[Self])
  }

  override def quickByIds(ids: List[String]): List[Self] = {
    super.quickByIds(ids) map (_.asInstanceOf[Self])
  }

  override def queryByIds(ids: List[String]): List[Self] = {
    super.queryByIds(ids) map (_.asInstanceOf[Self])
  }


  override def queryOne(sql: String, param: String*): Option[Self] = {
    super.queryOne(s"select * from $tableName where " + sql, param: _*) map (_.asInstanceOf[Self])
  }

  override def queryAll(): List[Self] = {
    super.queryAll map (_.asInstanceOf[Self])
  }

  override def query(where: String, param: String*): List[Self] = {
    super.query(where, param: _*) map (_.asInstanceOf[Self])
  }

  //这个接口需要传条件、排序
  override def queryPage(where: String, pageNum: Int, pageSize: Int, order: String, param: String*): (Int, List[Self]) = {
    val (count, list) = super.queryPage(where, pageNum, pageSize, order, param: _*)
    (count, list map (_.asInstanceOf[Self]))
  }

}

object BaseDBEntity {
  protected val map = new ObjectMapper() with ScalaObjectMapper
  map.registerModule(DefaultScalaModule)
  map.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
  map.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm"))


  def toJson(data: AnyRef) = {
    map.writeValueAsString(data)
  }
  def toHashMap(dbe:DBEntity): HashMap[String, Any]=BaseDBEntity.map.readValue(map.writeValueAsString(dbe), HashMap[String, Any]().getClass).asInstanceOf[HashMap[String, Any]]

  //自动检查数据的查询结果是否存在
  implicit class DBOptionAdd[T <: DBEntity](o: Option[T]) {
    def dbCheck: T = if (o.isEmpty) throw new DataNoFindExcepiton else o.get
  }

  def uuid = UUID.randomUUID().toString.replace("-", "")

}

import BaseDBEntity.uuid
/**
  *系统设置表，系统中重要设置全部存储于此表中
  *
  */
//系统参数
class Setting(val id: Int = -1, val name: String = "", val value: String = "",val remark:String="") extends BaseDBEntity[Setting]("Setting")

/**
  *管理员定义
  *Admin 是管理员  ，拥有组
  * RoleGroup 是组 ，每个组有不同的权限,其中 *　是所有权限
  *Role 是权限，限定每个操作
  */

//管理员表 定义
class Admin(val id: Int=0, val adminName: String="", val adminPassword: String="", val isEffect: Int=0, val roleId: Int=0, val createDate: Date=new Date) extends BaseDBEntity[Admin]("Admin")

//权限
class Role(val id: Int = -1, val path: String = "", val method: String = "", val name: String = "", val partten: String = "", val createDate: Date=new Date) extends BaseDBEntity[Role]("role")

//权限分组
class RoleGroup(val id: Int = -1, val name: String = "", val roles: String = "", val createDate: Date=new Date) extends BaseDBEntity[RoleGroup]("roleGroup")

//操作日志
class AdminModifyLog(val id: String=uuid, val aid: Int=0,val adminName:String="", val name: String="", val method: String="", val path: String="", val ip: String="", val createDate: Date=new Date) extends BaseDBEntity[AdminModifyLog]("AdminModifyLog")

class App(val id: Int = -1,val name:String="",val appid:String="", val appsecr: String ="",val dayLimit:Int=0,val allLimit:Int=0,val allMoney:Int=0,val rule:String="",val createTime: Date = new Date(System.currentTimeMillis())) extends BaseDBEntity[App]("App")

class UserConnect(val id: String =uuid,val name:String="",val appid:Int= -1,val icon:String="", val uid: String ="",val hasSend:Boolean=false,val token: String = "",val ext: String = "", val createTime: Date = new Date(System.currentTimeMillis())) extends BaseDBEntity[UserConnect]("UserConnect")

class User(val id:String=uuid,val name:String="",val icon:String="",val payToken:String="",val createTime: Date = new Date(System.currentTimeMillis())) extends BaseDBEntity[User]("User")

//订单  status  1成功 2 失败
class Order(val id:String=uuid,val uid:String="",val appid:Int= -1, val remark:String="",val amount:Int=0,val status:Int=0,val createDate:Date=new Date)extends BaseDBEntity[Order]("Orders")
